<template>
  <el-dialog
    :title="form.id?'编辑':'新增'"
    :visible.sync="dialogVisible"
    :before-close="close"
    width="30%"
  >
    <el-form label-width="120px">
      <el-form-item label="党员姓名">
        <el-select @change="personChange" v-model="form.partyMemberInfoMgmtId" size="mini" placeholder="请选择">
          <el-option
            v-for="item in personList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属党组织">
        <el-input disabled v-model="form.partyOrgName" size="mini" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="党内职务">
        <el-input disabled v-model="form.partyPositionsName" size="mini" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="批准任职时间">
        <el-date-picker
          v-model="form.approveAppointmentTime"
          size="mini"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addMember, editMember, queryPersonList, queryPersonById } from '@/api/partyMemberManagement/teamBuilding'
export default {
  data () {
    return {
      dialogVisible: false,
      form: {},
      personList: []
    }
  },
  methods: {
    open (row) {
      this.getPersonList()
      this.form = { ...row }
      this.dialogVisible = true
    },
    getPersonList () {
      queryPersonList().then((res) => {
        this.personList = res?.data || []
      })
    },
    confirm () {
      let submitForm = addMember
      if (this.form.id) {
        submitForm = editMember
      }
      const obj = {
        approveAppointmentTime: this.form.approveAppointmentTime,
        id: this.form.id || null,
        partyMemberInfoMgmtId: this.form.partyMemberInfoMgmtId,
        partyMemberName: this.form.partyMemberName
      }
      submitForm(obj).then(() => {
        this.form = {}
        this.$emit('getData')
        this.$message.success('保存成功')
        this.dialogVisible = false
      })
    },
    personChange (id) {
      queryPersonById({ id }).then((res) => {
        this.$set(this.form, 'partyMemberName', res?.data?.res?.data?.name)
        this.$set(this.form, 'partyOrgName', res?.data?.branchName)
        this.$set(this.form, 'partyPositionsName', res?.data?.partyPositionsName)
      })
    },
    close () {
      this.form = {}
      this.dialogVisible = false
    }
  }
}
</script>
